package com.chance.cc.crawler.prod.command.job.domain.commerce.du.product;

import com.alibaba.fastjson.JSON;
import com.chance.cc.crawler.core.CrawlerEnum;
import com.chance.cc.crawler.core.CrawlerJob;
import com.chance.cc.crawler.core.downloader.HttpConstant;
import com.chance.cc.crawler.core.downloader.HttpPage;
import com.chance.cc.crawler.core.downloader.HttpRequestBody;
import com.chance.cc.crawler.core.filter.FilterUtils;
import com.chance.cc.crawler.core.queue.crawler.CrawlerQueueConfig;
import com.chance.cc.crawler.core.record.CrawlerRequestRecord;
import com.chance.cc.crawler.meta.core.bean.CrawlerMetaConstant;
import com.chance.cc.crawler.meta.core.bean.job.CrawlerScheduleJob;
import com.chance.cc.crawler.prod.command.job.domain.commerce.du.DuCommonCrawlerJobScript;
import org.apache.commons.lang3.StringUtils;

import static com.chance.cc.crawler.core.CrawlerEnum.CrawlerRequestType.*;
import static com.chance.cc.crawler.core.CrawlerEnum.CrawlerRequestType.turnPage;

/**
 * @author lt
 * @version 1.0
 * @date 2021-06-22 10:21:00
 * @email okprog@sina.com
 */
public class DuProductDetailCrawlerSchedulerJob extends DuCommonCrawlerJobScript {
    public static final String metaSite = "product";
    public static final String startUrl = "api/v1/h5/commodity/fire/search/doCategoryDetail";

    public static void main(String[] args) {
        publishCrawlerScheduleJobInfo();
    }

    /**
     * 发布任务信息
     * @return
     */
    public static CrawlerJob publishCrawlerScheduleJobInfo(){

        CrawlerJob crawlerJob = crawlerScheduler();

        //发布定时任务
        CrawlerScheduleJob crawlerScheduleJob = new CrawlerScheduleJob();
        crawlerScheduleJob.setDomain(domain);
        crawlerScheduleJob.setCrawlerJob(JSON.toJSONString(crawlerJob));
        crawlerScheduleJob.setJobType(CrawlerMetaConstant.ScheduleCrawlerJobType.crawler.enumVal());
        crawlerScheduleJob.setNote("Du商品信息每日全量商品详情采集任务");
        crawlerScheduleJob.setCrawlerKey(crawlerJob.generateCrawlerKey());
        HttpPage httpPage = metaServiceCommand.addOrUpdateCrawlerScheduleJob(crawlerScheduleJob);
        System.out.println("发布任务：" + httpPage.getRawText());
        return crawlerJob;
    }

    public static CrawlerJob crawlerScheduler(){

        String requestQueueName = StringUtils.joinWith("-", "crawler", domain, metaSite, "productDetail", "queue");
        //文章采集
        CrawlerRequestRecord articleCrawler = doSearchNewsCrawler();

        //配置结果队列信息
        String resultDelQueueName = "delResult-du_app-spuIds";
        CrawlerQueueConfig redisDelResultQueue = new CrawlerQueueConfig(
                resultDelQueueName,
                CrawlerQueueConfig.Content.result,
                CrawlerQueueConfig.Storage.redis,
                CrawlerQueueConfig.Structure.list);



        CrawlerJob crawlerJob = CrawlerJob.builder()
                .triggerInfo(domain,
                        CrawlerMetaConstant.ScheduleJobTrigger_Cron,
                        System.currentTimeMillis(),
                        StringUtils.joinWith("-", domain,metaSite,"productDetail", CrawlerMetaConstant.ScheduleJobTriggerJob_Realtime))
                .crawlerRequestQueue(CrawlerMetaConstant.redisRequestQueue(requestQueueName))
                .kafkaResultPipeline("kafka_data_result",kafkaTopic,null)
                .queueResultPipeline("redis_del_result",redisDelResultQueue)
                .crawlerJobThreadNumber(5)
                .requestRecord(articleCrawler)
                .supportRecord(initCommentRecord(articleCrawler,requestQueueName))
//                .restWhileNon(false)
                .build();
        return crawlerJob;
    }

    public static CrawlerRequestRecord initCommentRecord(CrawlerRequestRecord startRequestRecord,String requestQueueName){
        CrawlerRequestRecord initRecord = CrawlerRequestRecord.builder()
                .itemPageRequest(startRequestRecord)
                .httpUrl("https://app.dewu.com/api/v1/h5/index/fire/flow/product/detail")
                .recordKey("https://app.dewu.com/api/v1/h5/index/fire/flow/product/detail")
                .releaseTime(System.currentTimeMillis())
                .needWashed(false)
                .needParsed(false)
                .copyBizTags()
                .build();
        String site = startRequestRecord.tagsCreator().bizTags().getCategoryTag().getKVTagStrVal("site");
        CrawlerRequestRecord initCommentCrawlerRecord = CrawlerRequestRecord.builder()
                .startPageRequest("du_app-result-all-init",turnPageItem)
                .httpUrl(metaServiceHttpPrefix + "/crawler/domain/common/api/v1/"+domain+"/results/queue/init?requestQueueName="
                        + requestQueueName + "&site=" + site)
                .requestLabelTag(supportSource)
                .requestLabelTag(internalDownload)
                .build();
        HttpRequestBody jsonBody = HttpRequestBody.json(JSON.toJSONString(initRecord), "utf-8");
        initCommentCrawlerRecord.getHttpRequest().setMethod(HttpConstant.Method.POST);
        initCommentCrawlerRecord.getHttpRequest().setRequestBody(jsonBody);
        return initCommentCrawlerRecord;
    }

    /**
     * 初始record
     * @return
     */
    public static CrawlerRequestRecord doSearchNewsCrawler(){

        CrawlerRequestRecord requestRecord = CrawlerRequestRecord.builder()
                .startPageRequest(domain, turnPage)
                .httpUrl(startUrl)
                .releaseTime(System.currentTimeMillis())
                .needWashed(false)
                .needParsed(false)
                .filter(CrawlerEnum.CrawlerRecordFilter.key)
                .addFilterInfo(FilterUtils.redisFilterKeyInfo("filter-du_app-spuIds"))
                .proxy(proxy)
                .build();

        requestRecord.getHttpConfig().setProxyQueueSize(50);
        requestRecord.getHttpConfig().setProxyType("queue");

        requestRecord.setDownload(false);
        requestRecord.setSkipPipeline(true);
        requestRecord.tagsCreator().bizTags().addSite(metaSite);
        requestRecord.tagsCreator().bizTags().addSiteBiz(metaSite);
        requestRecord.tagsCreator().bizTags().addCustomKV("crawler_type","productDetail");
        return requestRecord;
    }
}
